package com.example.ssyinitlearn.userDetailsImpl;

import com.example.ssyinitlearn.entity.RoleEntity;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;
import java.util.stream.Collectors;

@Data
public class UserEntityImpl implements UserDetails {
    /**
     * 数据库字段
     */
    private Long id;
    private String username;
    private String password;
    private Integer enabled;
    private Integer accountnonexpired;
    private Integer accountnonlocked;
    private Integer credentialsnonexpired;

    // 用户角色表
    private List<RoleEntity> roleEntityList = new ArrayList<>();

    /**
     * 获取当前用户的所有权限。在现在其实就是角色表
     * Returns the authorities granted to the user. Cannot return <code>null</code>.
     *
     * @return the authorities, sorted by natural key (never <code>null</code>)
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 将该user具备的role制作成SSY可识别的权限。
        ArrayList<GrantedAuthority> authorities = new ArrayList<>();
        for (RoleEntity roleEntity : this.roleEntityList) {
            // 不需要自己去考虑太多，就闭着眼往 new SimpleGrantedAuthority中传递String即可
            authorities.add(new SimpleGrantedAuthority(roleEntity.getName()));
        }

        return authorities;
    }

    /**
     * Returns the password used to authenticate the user.
     *
     * @return the password
     */
    @Override
    public String getPassword() {
        return this.password;
    }

    /**
     * Returns the username used to authenticate the user. Cannot return
     * <code>null</code>.
     *
     * @return the username (never <code>null</code>)
     */
    @Override
    public String getUsername() {
        return this.username;
    }

    /**
     * Indicates whether the user's account has expired. An expired account cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user's account is valid (ie non-expired),
     * <code>false</code> if no longer valid (ie expired)
     */
    @Override
    public boolean isAccountNonExpired() {
        // 为空或者1则返回true，为0返回false
        if(this.accountnonexpired == null){
            return true;
        }else return this.accountnonexpired == 1;
    }

    /**
     * Indicates whether the user is locked or unlocked. A locked user cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user is not locked, <code>false</code> otherwise
     */
    @Override
    public boolean isAccountNonLocked() {
        // 为空或者1则返回true，为0返回false
        if(this.accountnonlocked == null){
            return true;
        }else return this.accountnonlocked == 1;
    }

    /**
     * Indicates whether the user's credentials (password) has expired. Expired
     * credentials prevent authentication.
     *
     * @return <code>true</code> if the user's credentials are valid (ie non-expired),
     * <code>false</code> if no longer valid (ie expired)
     */
    @Override
    public boolean isCredentialsNonExpired() {
        // 为空或者1则返回true，为0返回false
        if(this.credentialsnonexpired == null){
            return true;
        }else return this.credentialsnonexpired == 1;
    }

    /**
     * Indicates whether the user is enabled or disabled. A disabled user cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user is enabled, <code>false</code> otherwise
     */
    @Override
    public boolean isEnabled() {
        // 为空或者1则返回true，为0返回false
        if(this.enabled == null){
            return true;
        }else return this.enabled == 1;
    }
}
